package ThreadPool.lagou.concurrent.java并发编程78讲.生产者消费者模型;

import javax.lang.model.element.Element;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 生产者消费者模型
 * @date 2022/5/28 21:16
 */
public class QueueList {

    // 自带阻塞功能环形队列
    private final BlockingQueue<Object> queue = new ArrayBlockingQueue<>(10,true);


    //生产方法
    public synchronized void put(Object o)  {
        while(true){
            try {
                queue.put(o);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //消费方法
    public synchronized void take(){
        while(true){
            queue.poll();
        }
    }

    public static void main(String[] args) {
        Runnable product = ()->{new QueueList().put(new Object());};

        Runnable consumer = ()->{new QueueList().take();};

        new Thread(product).start();

        new Thread(consumer).start();

    }
}
